<?php
namespace TennisTournament\Service;

use TennisTournament\Model\Security;
use TennisTournament\Persistent\AuthenticationDao;

class AuthenticationServiceImpl implements AuthenticationService {

    /**
     * @var AuthenticationDao
     */
    private $authenticationDao;

    public function __construct(AuthenticationDao $authenticationDao) {
        $this->authenticationDao = $authenticationDao;
    }

    /**
     * @param $id integer
     * @return an entity if have with such id or null otherwise
     * @throws \Exception
     */
    public function get($id) {
        // TODO: Implement get() method.
    }

    /**
     * Get {@link Security} by $username and $password
     * @param $username
     * @param $password
     * @return {@link Security}
     */
    public function getBy($username, $password) {
        $this->authenticationDao->startTransaction();
        try {
            /** @var $security Security  */
            $security = $this->authenticationDao->getBy($username, $password);

            $this->authenticationDao->commitTransaction();

            return $security;
        } catch(\Exception $ex) {
            $this->authenticationDao->rollbackTransaction();
            throw $ex;
        }
    }

    /**
     * Get all entities from specific type
     * @return Array from all entities from specific type
     * @throws \Exception
     */
    public function fetchAll() {
        $this->authenticationDao->startTransaction();
        try {
            $logins = $this->authenticationDao->fetchAll();

            $this->authenticationDao->commitTransaction();

            return $logins;
        } catch(\Exception $ex) {
            $this->authenticationDao->rollbackTransaction();
            throw $ex;
        }
    }

    /**
     * Save an specific entity;
     * @param entity
     * @return void
     * @throws \Exception
     */
    public function save($entity) {
        $this->authenticationDao->startTransaction();
        try {
            $this->authenticationDao->save($entity);

            $this->authenticationDao->commitTransaction();
        } catch (\Exception $ex) {
            $this->authenticationDao->rollbackTransaction();
            throw $ex;
        }
    }

    /**
     * Delete an specific entity;
     * @param entity
     * @return void
     * @throws \Exception
     */
    public function delete($entity) {
        // TODO: Implement delete() method.
    }

}